﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using _180FrameWork.Common;
using System.IO;
using System.Windows.Forms;

namespace _180FrameWork.CustControls
{
    public class CustDataGridViewStyleHelper
    {
        /// <summary>
        /// 样式字典
        /// </summary>
        static IDictionary<string, CustDataGridViewStyle> dict
        {
            get
            {
                if (_dict == null)
                {
                    //读取本地文件获取所有表格的自定义信息
                    if (File.Exists("custdatagridviewstyle.txt"))
                    {
                        string strinfo = MyFileHelper.Read("custdatagridviewstyle.txt");
                        _dict = strinfo.JsonToObj<IDictionary<string, CustDataGridViewStyle>>();
                    }
                    else
                    {
                        _dict = new Dictionary<string, CustDataGridViewStyle>();
                    }
                }
                return _dict;
            }
            set { _dict = value; }
        }
        static IDictionary<string, CustDataGridViewStyle> _dict;


        /// <summary>
        /// 获取样式
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static CustDataGridViewStyle GetStyle(string key)
        {
            if (dict.ContainsKey(key))
            {
                return dict[key];
            }
            else
                return null;
        }
        /// <summary>
        /// 保存样式
        /// </summary>
        /// <param name="style"></param>
        public static void SaveStyle(CustDataGridViewStyle style)
        {
            if (dict.ContainsKey(style.DataGridViewName))
            {
                dict[style.DataGridViewName] = style;
            }
            else
            {
                dict.Add(style.DataGridViewName, style);
            }
        }
        /// <summary>
        /// 保存样式字典
        /// </summary>
        public static void SaveStyleDict()
        {
            //保存到本地
            string strinfo = dict.ObjToJson();
            MyFileHelper.Write("custdatagridviewstyle.txt", strinfo, Encoding.UTF8, false);
        }
    }
}
